﻿@model UniTax.Models.TaxiModel

<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.8.24.js"></script>

@using (Ajax.BeginForm(new AjaxOptions
{
    UpdateTargetId = "ajaxContainer",
    OnBegin = "showWaitDiv",
    OnSuccess = "hideWaitDiv"
}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Taxi</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-field">
            @Html.EditorFor(model => model.TownId)
            @Html.ValidationMessageFor(model => model.TownId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Phones)
        </div>
        <div id="phoneList" class="editor-field">
            @foreach (var phone in Model.Phones)
            {   
                <img src="~/Images/icon/delete.png" />
                @Html.CheckBox("phoneCheck_" + phone.Id, new { @id = phone.Id, @style = "margin-right:10px", @onclick = "DeleteImgPhone(" + phone.Id + ")" })
                @Html.EditorFor(p => phone.Phone1)
                <img id="phoneDelImg_@phone.Id" style="position: relative; height: 27px; top: 10px;display:none" src="~/Images/icon/delCross.png" alt="Delete" />
                <br />
            }
            <input type="button" onclick="show_prompt(1)" value="Добавить" />
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ContactPhones)
        </div>
        <div id="contactPhoneList" class="editor-field">
            @foreach (var contactPhone in Model.ContactPhones)
            {
                <img src="~/Images/icon/delete.png" />
                @Html.CheckBox("contactPhoneCheck_" + contactPhone.Id, new { @id = contactPhone.Id, @style = "margin-right:10px", @onclick = "DeleteImgContactPhone(" + contactPhone.Id + ")" })
                @Html.EditorFor(p => contactPhone.Phone)
                <img id="contactPhoneDelImg_@contactPhone.Id" style="position: relative; height: 27px; top: 10px;display:none" src="~/Images/icon/delCross.png" />
                <br />
            }
            <input type="button" onclick="show_prompt(2)" value="Добавить" />
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ContactName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ContactName)
            @Html.ValidationMessageFor(model => model.ContactName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Functions)
        </div>
        <div id="functionList" class="editor-field">
            @foreach (var function in Model.AllFunctions)
            {
                @(Model.Functions.Contains(function)
                ? Html.CheckBox("func_" + function.Id, true, new { @name = function.Id, @style = "margin-right:10px" })
                : Html.CheckBox("func_" + function.Id, new { @name = function.Id, @style = "margin-right:10px" }))
                @Html.DisplayFor(p => function.Name)
                <br />
            }
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.License)
        </div>
        <div>
            @Html.TextBoxFor(model => model.License)
            @Html.ValidationMessageFor(model => model.License)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Logo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Logo)
            @Html.ValidationMessageFor(model => model.Logo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Raiting)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Raiting)
            @Html.ValidationMessageFor(model => model.Raiting)
        </div>

        <p>
            <input type="submit" value="Сохранить изменения" />
        </p>
    </fieldset>
}

<div>
    @Ajax.ActionLink("Назад к списку такси", "TaxiList", new { id = Model.TownId }, new AjaxOptions
{
    UpdateTargetId = "ajaxContainer",
    OnBegin = "showWaitDiv",
    OnSuccess = "hideWaitDiv"
})
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

@*Календарь*@
<script>
    $(function () {
        $('#License').datepicker({
            firstDay: 1,
            changeMonth: true,
            changeYear: true
        });

        $("#License").datepicker("option", "showAnim", "slideDown");
        $("#License").datepicker("option", "dateFormat", "dd/mm/yy");
        $("#License").datepicker("setDate", "@ViewBag.Date");

    });
</script>

@*Отмечает выбранные для удаления телефоны*@
<script type="text/javascript">
    function DeleteImgPhone(id) {
        if (document.getElementById("phoneDelImg_" + id).style.display == "none") {
            document.getElementById("phoneDelImg_" + id).style.display = "";
        } else {
            document.getElementById("phoneDelImg_" + id).style.display = "none";
        }
    }
    function DeleteImgContactPhone(id) {
        if (document.getElementById("contactPhoneDelImg_" + id).style.display == "none") {
            document.getElementById("contactPhoneDelImg_" + id).style.display = "";
        } else {
            document.getElementById("contactPhoneDelImg_" + id).style.display = "none";
        }
    }
</script>

@*Окно добавления телефона*@
<script>
    function show_prompt(type) {
        var phone = prompt("Введите номер телефона", "");
        if (phone != null && phone != "") {
            add_phone(phone, type);
        }
    }
    function add_phone(phone, type) {
        var list;
        if (type == 1)
            list = document.getElementById('phoneList');//если добавляeтся Phone
        else list = document.getElementById('contactPhoneList');//если добавляeтся ContactPhone
        // новый элемент
        var input = document.createElement('input');
        input.setAttribute("value", phone);
        input.setAttribute("type", 'text');

        var counter = 1;
        if (type == 1) {
            while (true) {
                if (document.getElementById('phone_new_' + counter) == null) {
                    input.setAttribute("id", 'phone_new_' + counter);
                    input.setAttribute("name", 'phone_new_' + counter);
                    break;
                } else counter = counter + 1;
            }
        } else {
            while (true) {
                if (document.getElementById('contactPhone_new_' + counter) == null) {
                    input.setAttribute("id", 'contactPhone_new_' + counter);
                    input.setAttribute("name", 'contactPhone_new_' + counter);
                    break;
                } else counter = counter + 1;
            }
        }
        // добавление в конец
        list.appendChild(input);
    }
</script>
